package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.view.*;
import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

/**
 * OrderFormapi
 */
 
@Api(tags = "OrderForm",description = "订单")
@RestController
public class OrderFormController {
    
    @Autowired
    private OrderFormDao dao;
   
    /**
     * 获取订单集合
     * @return api结果
     */
    @ApiOperation(value = "获取订单集合")
    @RequestMapping(value =  "/api/OrderForm/",method = {RequestMethod.GET})
    public ApiResult<List<OrderForm>> Get(){
         ApiResult<List<OrderForm>> result=new ApiResult<List<OrderForm>>();
        try 
        {
            List<OrderForm> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取订单信息
     * @param id OrderFormID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取订单信息")
    @RequestMapping(value =  "/api/OrderForm/{id}",method = {RequestMethod.GET})
    public ApiResult<OrderForm> Get(@PathVariable Long id){
        ApiResult<OrderForm> result=new ApiResult<OrderForm>();
        try
        {
            OrderForm model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改订单
     * @param id OrderFormID
     * @param model 订单
     * @return api结果
     */
    @ApiOperation(value = "修改订单")
    @RequestMapping(value =  "/api/OrderForm/{id}",method = {RequestMethod.PUT})
    public ApiResult<OrderForm> Edit(@PathVariable Long id,@RequestBody OrderForm model){
        ApiResult<OrderForm> result=new ApiResult<OrderForm>();
        try
        {
            model.setOrderFormId(id);
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加订单
     * @param model 订单
     * @return api结果
     */
    @ApiOperation(value = "添加订单")
    @RequestMapping(value =  "/api/OrderForm/",method = {RequestMethod.POST})
    public ApiResult<OrderForm> Add(OrderForm model){
         ApiResult<OrderForm> result=new ApiResult<OrderForm>();
        try
        {
            model.setCreateDate(new Date());
            model.setIsPay(0L);
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 查询订单分页
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询所有订单")
    @RequestMapping(value =  "/api/OrderForm/SearchAll",method = {RequestMethod.POST})
    public ApiResult<Object> SearchAll(@RequestBody PageDate model){
         ApiResult<Object> result=new ApiResult<Object>();
         
        try
        {    
            Long start=model.getStartDate().getTime()/1000;
            Long end=model.getEndDate().getTime()/1000;            
            List< Object> page = dao.findByWhere(model.getProjectId(),start,end);
            
            result.setData(page);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }


    /**
     * 查询订单分页
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询订单分页")
    @RequestMapping(value =  "/api/OrderForm/Search",method = {RequestMethod.POST})
    public ApiResult<List<OrderForm>> Search(@RequestBody PageDate model){
         ApiResult<List<OrderForm>> result=new ApiResult<List<OrderForm>>();
         
        try
        {    
            Long start=model.getStartDate().getTime()/1000;
            Long end=model.getEndDate().getTime()/1000;
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"order_Form_Id") ;            
            Page<OrderForm> page = dao.findByWhere(model.getProjectId(),model.getName(),model.getPhone(),start,end,pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 删除订单
     * @param id OrderFormID
     * @return api结果
     */
    @ApiOperation(value = "删除订单")
    @RequestMapping(value =  "/api/OrderForm/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

